package net.fdukedom.epicurus.domain.entity;

import java.util.HashSet;
import java.util.Set;

/**
 * Handles basic logic and properties of plug-in.
 *
 * @author Dmitry Y. Kochelaev
 */
public final class Plugin {

    /**
     * Handler for messages.
     */
    private String recipient = null;

    /**
     * Cleint plug-in class name.
     */
    private String clientPlugin = null;

    /**
     * WSEndpoint class.
     */
    private String wsEndpoint = null;

    /**
     * Plugin id.
     */
    private int id;

    /**
     * Plugin name.
     */
    private String name = null;

    /**
     * Plugin version.
     */
    private int version = 0;

    /**
     * Message types.
     */
    private Set<MessageType> messageTypes = new HashSet<MessageType>();

    /**
     * Message types.
     */
    private Set<Page> pages = new HashSet<Page>();
    
    /**
     * Extension points.
     */
    private Set<ExtensionPoint> extensionPoints = new HashSet<ExtensionPoint>();

    /**
     * Actions.
     */
    private Set<Action> actions = new HashSet<Action>();

    /**
     * Groups.
     */
    private Set<Group> groups = new HashSet<Group>();
    
    /**
     * Action rules.
     */ 
    private Set<AccessRule> accessRules = new HashSet<AccessRule>(); 

    /**
     * Action rules.
     */ 
    private Set<Extension> extensions = new HashSet<Extension>(); 
    
    /**
     * Default constructor. Required by hibernate.
     */
    public Plugin() {

    }

    /**
     * Returns messages handler.
     *
     * @return messages handler
     */
    public String getRecipient() {
        return recipient;
    }

    /**
     * Sets messages handler.
     *
     * @param name new messages handler name
     *
     */
    public void setRecipient(String name) {
       recipient = name;
    }

    /**
     * Returns client plug-in class name.
     *
     * @return client plug-in class name
     */
    public String getClientPlugin() {
        return clientPlugin;
    }

    /**
     * Sets client plug-in class name
     *
     * @param clientPlugin client plug-in class name
     */
    public void setClientPlugin(String clientPlugin) {
        this.clientPlugin = clientPlugin;
    }

    /**
     * Returns ws-endpoint.
     *
     * @return ws-endpoint
     */
    public String getWsEndpoint() {
        return wsEndpoint;
    }

    /**
     * Sets ws-endpoint.
     *
     * @param wsEndpoint endpoint to set
     */
    public void setWsEndpoint(String wsEndpoint) {
        this.wsEndpoint = wsEndpoint;
    }

    /**
     * Returns id.
     *
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * Sets id.
     *
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * Returns name.
     *
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * Sets name.
     *
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }


    /**
     * Returns messageTypes.
     *
     * @return the messageTypes
     */
    public Set<MessageType> getMessageTypes() {
        return messageTypes;
    }


    /**
     * Sets mesageTypes.
     *
     * @param messageTypes the messageTypes to set
     */
    public void setMessageTypes(Set<MessageType> messageTypes) {
        this.messageTypes = messageTypes;
    }

    /**
     * Adds messageType.
     *
     * @param messageType the messageType to add
     */
    public void addMessageType(MessageType messageType) {
        messageType.setPlugin(this);
    }

    /**
     * @param pages the messageTypes to set
     */
    public void setPages(Set<Page> pages) {
        this.pages = pages;
    }

    /**
     * Adds page.
     *
     * @param page the page to add
     */
    public void addPage(Page page) {
        page.setPlugin(this);       
    }

    /**
     * Returns pages.
     *
     * @return the pages
     */
    public Set<Page> getPages() {
        return pages;
    }

    /**
     * @param groups the groups to set
     */
    public void setGroups(Set<Group> groups) {
        this.groups = groups;
    }

    /**
     * Adds group.
     *
     * @param group the group to add
     */
    public void addGroup(Group group) {
        group.setPlugin(this);       
    }

    /**
     * Returns groups.
     *
     * @return the groups
     */
    public Set<Group> getGroups() {
        return groups;
    }

    /**
     * Returns version.
     *
     * @return the version
     */
    public int getVersion() {
        return version;
    }


    /**
     * Sets version.
     * @param version the version to set
     */
    public void setVersion(int version) {
        this.version = version;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final Plugin plugin = (Plugin) o;

        if (version != plugin.version) return false;
        if (name != null ? !name.equals(plugin.name) : plugin.name != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (name != null ? name.hashCode() : 0);
        result = 29 * result + version;
        return result;
    }

    /**
     * @return the extensionPoints
     */
    public Set<ExtensionPoint> getExtensionPoints() {
        return extensionPoints;
    }

    /**
     * @param extensionPoints the extensionPoints to set
     */
    public void setExtensionPoints(Set<ExtensionPoint> extensionPoints) {
        this.extensionPoints = extensionPoints;
    }

    /**
     * @return the actions
     */
    public Set<Action> getActions() {
        return actions;
    }

    /**
     * @param actions the actions to set
     */
    public void setActions(Set<Action> actions) {
        this.actions = actions;
    }

    public String toString() {
        StringBuilder text = new StringBuilder();
        text.append("{id: ");
        text.append(id);
        text.append(", name: ");
        text.append(name);
        text.append(", version: ");
        text.append(version);
        text.append(", recipient class: ");
        text.append(recipient);
        text.append("}");
        return text.toString();
    }


    /**
     * @return the accessRules
     */
    public Set<AccessRule> getAccessRules() {
        return accessRules;
    }

    /**
     * @param accessRules the accessRules to set
     */
    public void setAccessRules(Set<AccessRule> accessRules) {
        this.accessRules = accessRules;
    }

    /**
     * @return the extensions
     */
    public Set<Extension> getExtensions() {
        return extensions;
    }

    /**
     * @param extensions the extensions to set
     */
    public void setExtensions(Set<Extension> extensions) {
        this.extensions = extensions;
    }
}
